<?php
/*
 *   This file is part of poat.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *   Copyright (c) 2008 Sergio Gabriel Teves <gabriel.sgt at gmail.com>
 * 
 */
 
class util {

	public static function decode($text) {
		//debug("decode " . $text);
		return html_entity_decode($text,ENT_QUOTES,"UTF-8");
	}
	
	public static function encode($text) {
		//debug("encode " . $text);
		return htmlentities($text,ENT_QUOTES,"UTF-8");
	}

    public static function nathis($text) {
        if (empty($text)) {
          $text = "N/A";
        }
        return $text;
    }	

	public static function str($text) {
	  return "\"".$text."\"";
	}

    public static function fillparam() {
		global $_GET;
		global $_POST;

		$array1=$_GET;
		$array2=$_POST;
		
        $res = array();

        foreach ($array1 as $nombrevar => $valorvar) {
            if (!empty($valorvar)) {
              $res[$nombrevar] = $valorvar;
            }
        }

        foreach ($array2 as $nombrevar => $valorvar) {
            if (!empty($valorvar)) {
              $res[$nombrevar] = $valorvar;
            }
        }

        return $res;
    }

	public static function encrypt($text) {
		//return md5($text);
		return sha1($text);
	}

	public static function now() {
		$now = time();	
		return util::formatDate($now);   
	}	
	
	public static function formatDate($now) {
		return date('YmdHis',$now);
	}
	
	public static function mysqlToDate($fmt,$fecha) {
		return date($fmt,strtotime($fecha));
	}
	
	public static function fromUTC($datetime, $zone) {
		if ($datetime == null) {
			return "";
		} 
		$ntime = strtotime($datetime) + (($zone/100)*60*60);
		$ntime = date('Y-m-d H:i',$ntime);
		return $ntime;
	}
	
	public static function toUTC($datetime, $zone) {
		if ($datetime == null) {
			return "";
		} 
		//$ntime = strtotime($datetime) - (($zone/100)*60*60);
		$ntime = $datetime - (($zone/100)*60*60);
		//$ntime = date('Y-m-d H:i:s',$ntime);
		return util::formatDate($ntime);
	}

	public static function hashcode($text) {
		$text = time().$text.rand(10000,99999);
		return util::encrypt($text);
	}
	
	public static function createPassword($len) {
		$chars = "abcdefghijkmnopqrstuvwxyz023456789";
		srand((double)microtime()*1000000);
		$i = 0;
	    $pass = '' ;

	    while ($i <= $len) {
	        $num = rand() % 33;
	        $tmp = substr($chars, $num, 1);
	        $pass = $pass . $tmp;
	        $i++;
	    }
	    return $pass;
	}
	
	public static function isExpire($xdate,$period) {
		return (strtotime($xdate) + ($period*60*60)<time());
	}
	
	public static function expireDate($period) {
		return (time() + ($period*60*60));
	}
	
	public static function arrayToString($array, $separator = ";") {
		$tmp="";
		for ($i=0;$i<count($array);$i++) {
			if ($i==0) {
				$tmp=$array[$i];
			} else {
				$tmp .=$separator.$array[$i];
			}
		}
		return $tmp;
	}

	public static function keyArrayToString($array, $separator = ";") {
		$tmp="";
		foreach ($array as $k => $v) {
			if ($tmp=="") {
				$tmp=$k."=>".$v;
			} else {
				$tmp .=$separator.$k."=>".$v;
			}
		}
		return $tmp;
	}
	
	public static function stringToArray($string, $separator = ";") {
		$tmp = explode($separator,$string);
		return $tmp;
	}
	
	public static function cleanbreaklines($text) {
		$text= str_replace("\r\n", "", $text);
		$text= str_replace("\r", "", $text);
		$text= str_replace("\n", "", $text);
		return $text;
	}
	
}

?>
